package com.ruoyi.hxf.controller;

import java.util.List;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;

import com.ruoyi.common.helper.LoginHelper;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.vo.SysOssVo;
import com.ruoyi.system.service.ISysOssService;
import lombok.RequiredArgsConstructor;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;

import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.hxf.domain.vo.SchoolVo;
import com.ruoyi.hxf.domain.bo.SchoolBo;
import com.ruoyi.hxf.service.ISchoolService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 学校介绍管理
 *
 * @author hxf
 * @date 2023-04-07
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/school/show")
public class SchoolController extends BaseController {
    private final ISysOssService iSysOssService;
    private final ISchoolService iSchoolService;

    /**
     * 查询学校介绍管理列表
     */
//    @SaCheckPermission("school:show:list")
    @GetMapping("/list")
    public TableDataInfo<SchoolVo> list(SchoolBo bo, PageQuery pageQuery) {
        return iSchoolService.queryPageList(bo, pageQuery);
    }
    /**
     * 查询学校介绍管理列表
     */
    @GetMapping("/appList")
    public TableDataInfo<SchoolVo> appList(SchoolBo bo, PageQuery pageQuery) {
        return iSchoolService.queryPageAppList(bo, pageQuery);
    }
    /**
     * 导出学校介绍管理列表
     */
//    @SaCheckPermission("school:show:export")
    @Log(title = "学校介绍管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(SchoolBo bo, HttpServletResponse response) {
        List<SchoolVo> list = iSchoolService.queryList(bo);
        ExcelUtil.exportExcel(list, "学校介绍管理", SchoolVo.class, response);
    }

    /**
     * 获取学校介绍管理详细信息
     *
     * @param schoolId 主键
     */
//    @SaCheckPermission("school:show:query")
    @GetMapping("/{schoolId}")
    public R<SchoolVo> getInfo(@NotNull(message = "主键不能为空")
                               @PathVariable Long schoolId) {
        return R.ok(iSchoolService.queryById(schoolId));
    }

    /**
     * 新增学校介绍管理
     */
//    @SaCheckPermission("school:show:add")
    @Log(title = "学校介绍管理", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody SchoolBo bo) {
        if (bo.getSchoolPicture() != null) {
            List<SysOssVo> list = iSysOssService.listByIds(Arrays.asList(Long.parseLong(bo.getSchoolPicture())));
            bo.setSchoolUrl(list.get(0).getUrl());
        }

        return toAjax(iSchoolService.insertByBo(bo));
    }

    /**
     * 修改学校介绍管理
     */
//    @SaCheckPermission("school:show:edit")
    @Log(title = "学校介绍管理", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody SchoolBo bo) {
        if (bo.getSchoolPicture() != null) {
            List<SysOssVo> list = iSysOssService.listByIds(Arrays.asList(Long.parseLong(bo.getSchoolPicture())));
            bo.setSchoolUrl(list.get(0).getUrl());
        }else {
            bo.setSchoolUrl(null);
        }
        bo.setUpdateBy(LoginHelper.getUsername());
        bo.setUpdateTime(DateUtils.getNowDate());
        return toAjax(iSchoolService.myUpdateSchool(bo));
//        return toAjax(iSchoolService.updateByBo(bo));
    }

    /**
     * 删除学校介绍管理
     *
     * @param schoolIds 主键串
     */
//    @SaCheckPermission("school:show:remove")
    @Log(title = "学校介绍管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{schoolIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] schoolIds) {
        return toAjax(iSchoolService.deleteWithValidByIds(Arrays.asList(schoolIds), true));
    }
}
